package com.hua.common.commonview;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;

import com.hua.common.commontools.DipUtil;

/**
 * 水滴控件
 * @author huaxiaolin_pc
 */
@SuppressLint("DrawAllocation")
public class DropControl extends LinearLayout implements
		RefreshLayout.HeardViewRefreshListener {

	/**
	 * 用于绘制圆形的画笔
	 */
	private Paint ringPaint = null;
	/**
	 * 绘制圆环的画笔
	 */
	private Paint arcPaint = null;
	/**
	 * 用于绘制线条的画笔
	 */
	private Paint linePaint = null;
	private Paint linePaint2 = null;

	/**
	 * 控件的宽度
	 */
	private float viewWidth = 0f;
	/**
	 * 控件中心点
	 */
	private float X0 = 0f;

	/**
	 * 上边大圆的圆心位置
	 */
	private float X1 = 0f;
	private float Y1 = 0f;
	/**
	 * 下边小圆的圆心位置
	 */
	private float X2 = 0f;
	private float Y2 = 0f;

	/**
	 * 下拉的高度
	 */
	private float moveY = 0f;
	/**
	 * 下拉刷新控件总共可以下移的高度
	 */
	private float moveCountY = 0f;

	/**
	 * 上边的圆点的半径
	 */
	private float topR = 0f;
	/**
	 * 上边距
	 */
	private float paddingTop = 0f;

	/**
	 * 刷新时 上边转圈的圆环的
	 */
	private float point = 0f;
	private float count = 0f;
	private AnimatorSet animatorSet = null;

	private int stat = 0;

	public DropControl(Context context) {
		super(context);
		init();
	}

	public DropControl(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public DropControl(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	/**
	 * 初始化函数
	 */
	private void init() {
		this.initPaint();
	}

	/**
	 * 初始化画笔
	 */
	private void initPaint() {
		ringPaint = new Paint();
		ringPaint.setAntiAlias(true);
		ringPaint.setStyle(Style.FILL);
		ringPaint.setStrokeWidth(5);
		ringPaint.setAntiAlias(true); // 消除锯齿
		ringPaint.setDither(true);
		ringPaint.setStrokeCap(Cap.ROUND); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
		// Cap.ROUND,或方形样式Cap.SQUARE
		ringPaint.setColor(Color.parseColor("#ff0085cf"));

		linePaint = new Paint();
		linePaint.setAntiAlias(true);
		linePaint.setStyle(Style.STROKE);
		linePaint.setStrokeWidth(0.01f);
		linePaint.setAntiAlias(true); // 消除锯齿
		linePaint.setDither(true);
		linePaint.setStrokeCap(Cap.BUTT); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
		// Cap.ROUND,或方形样式Cap.SQUARE
		linePaint.setColor(Color.parseColor("#ff0085cf"));

		linePaint2 = new Paint();
		linePaint2.setAntiAlias(true);
		linePaint2.setStyle(Style.FILL);
		linePaint2.setStrokeWidth(5f);
		linePaint2.setAntiAlias(true); // 消除锯齿
		linePaint2.setDither(true);
		linePaint2.setStrokeCap(Cap.BUTT); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
		// Cap.ROUND,或方形样式Cap.SQUARE
		linePaint2.setColor(Color.parseColor("#ff0085cf"));

		arcPaint = new Paint();
		arcPaint.setAntiAlias(true);
		arcPaint.setStyle(Style.STROKE);
		arcPaint.setStrokeWidth(DipUtil.Dp2px(getContext(), 2));
		arcPaint.setAntiAlias(true); // 消除锯齿
		arcPaint.setDither(true);
		arcPaint.setStrokeCap(Cap.BUTT); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
		// Cap.ROUND,或方形样式Cap.SQUARE
		arcPaint.setColor(Color.parseColor("#ffffffff"));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (stat == 0) {
			this.drawDrop(canvas);
		} else if (stat == 1) {
			RectF oval1 = new RectF(X1 - topR, Y1 - topR, X1 + topR, Y1 + topR);
			canvas.drawArc(oval1, 270,
					(this.moveY / (this.moveCountY / 2)) * 360, true, ringPaint);// 小弧形
			this.drawArc(canvas);
		}
	}

	/**
	 * 绘制水滴
	 *
	 * @param canvas
	 */
	private void drawDrop(Canvas canvas) {
		float tempY = (this.moveY - this.moveCountY / 2);
		X2 = X0;
		Y2 = Y1 + tempY;
		float r = (float) (topR - tempY * 0.2);
		if (this.moveY >= 0 && this.moveY < this.moveCountY / 2) {
			RectF oval1 = new RectF(X1 - topR, Y1 - topR, X1 + topR, Y1 + topR);
			canvas.drawArc(oval1, 270,
					(this.moveY / (this.moveCountY / 2)) * 360, true, ringPaint);// 小弧形
		} else {
			float tempTopR = topR - tempY * 0.02f;
			RectF oval1 = new RectF(X1 - tempTopR, Y1 - tempTopR,
					X1 + tempTopR, Y1 + tempTopR);
			canvas.drawArc(oval1, 0, 360, true, ringPaint);// 大圆
			RectF oval2 = new RectF(X2 - r, Y2 - r, X2 + r, Y2 + r);
			canvas.drawArc(oval2, 0, 360, false, ringPaint);// 小圆
			float tempOffset = tempY * 0.1f;

			Path path = new Path();
			path.moveTo(X1 - tempTopR, Y1);
			path.quadTo((X1 + X2 - r - tempTopR) / 2 + tempOffset,
					(Y1 + Y2) / 2, X2 - r, Y2);
			canvas.drawPath(path, linePaint);
			path.lineTo(X2 + r, Y2);
			path.quadTo((X2 + X1 + r + tempTopR) / 2 - tempOffset,
					(Y1 + Y2) / 2, X1 + tempTopR, Y1);
			canvas.drawPath(path, linePaint);
			path.close();
			canvas.drawPath(path, linePaint2);
			dynamicDrawArc(canvas, (360) * (tempY / (this.moveCountY / 2)));
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		this.viewWidth = this.getMeasuredWidth();
		this.X0 = this.viewWidth / 2;
	}

	@Override
	public void down() {

	}

	@Override
	public void move(float subY, float countY) {
		moveY = subY;
		moveCountY = countY;
		topR = moveCountY / 7f;
		paddingTop = topR / 2;
		X1 = X0;
		Y1 = paddingTop + topR;
		this.invalidate();
	}

	@Override
	public void up() {
		if (this.moveY > this.moveCountY / 2) {
			this.RecoveryAnim();
		} else {
			this.sectorAnim();
		}
	}

	@Override
	public void reFreshing() {
		if (this.animatorSet == null) {
			this.initAnim();
		}
		stat = 1;
		this.animatorSet.start(); // 启动圆环旋转动画
	}

	@Override
	public void complete() {
		stat = 0;
	}

	/**
	 * 水滴弹回动画
	 */
	private void RecoveryAnim() {
		ValueAnimator anim = ValueAnimator.ofFloat(this.moveY,
				this.moveCountY / 2);
		anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				moveY = ((float) animation.getAnimatedValue());
				invalidate();
			}
		});
		anim.setDuration(300);
		anim.start();
	}

	/**
	 * 圆形回复动画
	 */
	private void sectorAnim() {
		ValueAnimator anim = ValueAnimator.ofFloat(this.moveY, 0);
		anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				moveY = ((float) animation.getAnimatedValue());
				System.out.println(moveY + "");
				invalidate();
			}
		});
		anim.setDuration(300);
		anim.start();
	}

	/**
	 * 头部圆环旋转动画
	 */
	private void initAnim() {
		ValueAnimator pointAnimator = ValueAnimator.ofFloat(270, 630);
		pointAnimator.setRepeatCount(Integer.MAX_VALUE);
		pointAnimator.setRepeatMode(ValueAnimator.RESTART);
		pointAnimator
				.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
					@Override
					public void onAnimationUpdate(ValueAnimator animation) {
						point = (float) animation.getAnimatedValue();
						invalidate();
					}

				});
		pointAnimator.setInterpolator(new LinearInterpolator());
		ValueAnimator countAnimator = ValueAnimator.ofFloat(0,360 );
		countAnimator.setRepeatCount(Integer.MAX_VALUE);
		countAnimator.setRepeatMode(ValueAnimator.RESTART);
		countAnimator
				.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
					@Override
					public void onAnimationUpdate(ValueAnimator animation) {
						count = (float) animation.getAnimatedValue();
						invalidate();
					}

				});
		countAnimator.setInterpolator(new LinearInterpolator());
		this.animatorSet = new AnimatorSet();
		this.animatorSet.playTogether(pointAnimator, countAnimator);
		this.animatorSet.setDuration(1500);
	}

	/**
	 * 绘制刷新旋转圆环
	 *
	 * @param canvas
	 */
	private void drawArc(Canvas canvas) {
		RectF oval1 = new RectF(X1 - topR + paddingTop, 2 * paddingTop, X1
				+ topR - paddingTop, Y1 + topR - paddingTop);
		canvas.drawArc(oval1, point, count, false, arcPaint); // 小弧形
	}

	/**
	 * 动态绘制刷新旋转圆环
	 *
	 * @param canvas
	 */
	private void dynamicDrawArc(Canvas canvas, float count) {
		RectF oval1 = new RectF(X1 - topR + paddingTop, 2 * paddingTop, X1
				+ topR - paddingTop, Y1 + topR - paddingTop);
		canvas.drawArc(oval1, 270, count, false, arcPaint); // 小弧形
	}
}
